import { http, HttpResponse } from "msw";
import { faker } from "@faker-js/faker";
import { UserApi } from "@/api/service/userService";
import { USER_LIST, changeUser, Initialization, resetData,doRegister } from "./assets";

// 接口测试
const testing = http.get("/api/testing", () => {
  console.log("Testing");
  return HttpResponse.json({
    message: "数据请求成功！",
    data: {
      name: "罗雨",
      age: 20,
    },
    status: 200,
  });
});
// 用户发起的请求到这里进行处理，rest进行了一波拦截
const login = http.post(
  `/api${UserApi.Login}`,
  async ({ request, params, cookies }) => {
    const { username, password } = await request.json();

    const user = USER_LIST.find((item) => item.username === username);

    if (!user || user.password !== password) {
      return HttpResponse.json({
        message: "登录失败！",
        status: 10003,
      });
    }
    changeUser(user);
    Initialization();
    return HttpResponse.json({
      status: 200,
      message: "登录成功！",
      data: {
        ...user,
        // token: faker.string.uuid(),
        // 双token用户登录策略先留着
        // accessToken: faker.string.uuid(),
        // refreshToken: faker.string.uuid(),
      },
    });
  }
);

const register = http.post(
  `/api${UserApi.Register}`,
  async ({ request, params, cookies }) => {
    const newUser = await request.json();

    const user = USER_LIST.find((item) => item.username === newUser.username);

    if (user) {
      return HttpResponse.json({
        message: "注册失败,该用户名已存在！",
        status: 10003,
      });
    }
    doRegister(newUser);
    return HttpResponse.json({
      status: 200,
      message: "注册成功！",
      data: {},
    });
  }
);

const validate = http.post(
  `/api${UserApi.Validate}`,
  async ({ request, params, cookies }) => {
    const { phone } = await request.json();

    const user = USER_LIST.find((item) => item.phone === phone);

    if (!user) {
      return HttpResponse.json({
        message: "该手机号不存在,请重新输入！",
        status: 10003,
      });
    }

    return HttpResponse.json({
      status: 200,
      message: "验证码已发送！",
      data: {
        code: Math.floor(1000 + Math.random() * 9000).toString(),
      },
    });
  }
);

const reset = http.post(
  `/api${UserApi.Reset}`,
  async ({ request, params, cookies }) => {
    const { password } = await request.json();

    if (password === "") {
      return HttpResponse.json({
        message: "修改密码失败！",
        status: 10003,
      });
    }

    return HttpResponse.json({
      status: 200,
      message: "修改密码成功！",
      data: {},
    });
  }
);

const logout = http.get(
  `/api${UserApi.Logout}`,
  async ({ request, params, cookies }) => {
    resetData(); //重置数据
    return HttpResponse.json({
      status: 200,
      message: "退出登录成功！",
      data: {},
    });
  }
);

const update = http.post(
  `/api${UserApi.Update}`,
  async ({ request, params, cookies }) => {
    const { avatar } = await request.json();

    // 创建一个 Blob 对象
    const blob = new Blob([avatar], { type: "multipart/form-data" });
    // 使用 URL.createObjectURL() 方法创建 URL
    const url = URL.createObjectURL(blob);

    if (!url) {
      return HttpResponse.json({
        message: "修改头像失败!",
        status: 10003,
      });
    }

    return HttpResponse.json({
      status: 200,
      message: "修改头像成功！",
      data: {
        avatar: url,
      },
    });
  }
);

const userArr = [testing, login, register, validate, reset, logout, update];
export default userArr;
